package com.zz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zz.common.Log;
import com.zz.common.R;
import com.zz.entity.*;
import com.zz.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 社团负责人管理
 * @author 朱彦燕
 */
@RestController
@Slf4j
@RequestMapping("/leader")
@CrossOrigin
@Validated
public class LeaderController {
    @Autowired
    private ActivityService activityService;

    @Autowired
    private ClubService clubService;

    @Autowired
    private ApplicationAuditingService applicationAuditingService;

    @Autowired
    private ClubAuditingService clubAuditingService;

    @Autowired
    private ClubLeaderService clubLeaderService;

    /**
     * 展示管理的所有社团
     * */
    @RequiresRoles("leader")
    @GetMapping("/manageClubList")
    @Log(title = "展示管理的所有社团", dataLocationType = "link")
    R<List<Club>> manageClubList(HttpServletRequest request){
        return clubService.manageClubList(request);
    }

    /**
     * 选择将要管理的社团
     * 将社团id 存到session中
     * 朱彦燕
     * */
    @RequiresRoles("leader")
    @PostMapping("/chooseClub")
    //@CacheEvict(value = "draftCache",allEntries = true)
    @Caching(evict={@CacheEvict(value = "draftCache",allEntries = true),
            @CacheEvict(value = "clubCache",allEntries = true),
            @CacheEvict(value = "boxCache",allEntries = true)})
    @Log(title = "社团负责人：选择将要管理的社团", dataLocationType = "link")
    public R<String> chooseClub(@NotNull(message = "社团编码不能为空") @RequestParam int clubId,
                                HttpServletRequest request){
        return clubService.chooseClub(clubId, request);
    }

    /**
     * 退出对本社团的管理
     * 朱彦燕
     * 测试成功
     * */
    @RequiresRoles("leader")
    @PostMapping("/logout")
    @Log(title = "社团负责人：退出对本社团的管理")
    public R<String> clubLogout(HttpServletRequest request){
        return clubService.clubLogout(request);
    }

    /**
     * 修改社团信息：社团名称和编码不允许修改
     * */
    @PutMapping("/editClub")
    @RequiresRoles("leader")
    @Log(title = "社团负责人：修改社团信息", dataLocationType = "body")
    public R<String> editClub(@RequestBody Club club){
        return clubService.editClub(club);
    }

    /**
     * 同意活动报名申请
     * 朱彦燕
     * @param applicationAuditing
     * 测试成功
     * */
    @RequiresRoles("leader")
    @PutMapping("/agree")
    @Log(title = "社团负责人：同意活动报名申请", dataLocationType = "body")
    public R<String> agreeApply(@RequestBody @Validated(Delete.class) ApplicationAuditing applicationAuditing){
        return applicationAuditingService.agreeApply(applicationAuditing);
    }

    /**
     * 不同意活动报名申请
     * 朱彦燕
     * @param applicationAuditing
     * 测试成功
     * */
    @RequiresRoles("leader")
    @PutMapping("/notAgree")
    @Log(title = "社团负责人：不同意活动报名申请", dataLocationType = "body")
    public R<String> notAgreeApply(@RequestBody @Validated(Delete.class) ApplicationAuditing applicationAuditing){
        return applicationAuditingService.notAgreeApply(applicationAuditing);
    }

    /**
     * 分页展示报名申请列表
     * 朱彦燕
     * @param page
     * @param pageSize
     * 测试成功
     * */
    @RequiresRoles("leader")
    @GetMapping("/applyList")
    @Log(title = "社团负责人：分页展示报名申请列表", dataLocationType = "link")
    public R<Page> applyList(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                             @RequestParam @NotNull(message = "每页最大值不能为空") int pageSize,
                             HttpServletRequest request){
        return applicationAuditingService.applyList(page, pageSize, request);
    }

    /**
     * 分页展示加入社团的申请
     * */
    @RequiresRoles("leader")
    @GetMapping("/showJoinClub")
    @Log(title = "分页展示加入社团的申请",dataLocationType = "link")
    R<Page> pageJoinClub(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                 @RequestParam @NotNull(message = "每页最大值不能为空") int pageSize,
                 HttpServletRequest request){
        return clubAuditingService.page(page, pageSize, request);
    }

    /**
     * 同意用户加入社团
     * */
    @RequiresRoles("leader")
    @PutMapping("/agreeJoin")
    R<String> agreeJoin(@NotNull(message = "社团审核表主键不能为空") @RequestParam int id){
        log.info("主键id:{}", id);
        return clubAuditingService.agreeJoin(id);
    }

    /**
     * 不同意用户加入社团
     * */
    @RequiresRoles("leader")
    @PutMapping("/notAgreeJoin")
    @Log(title = "不同意用户加入社团",dataLocationType = "link")
    R<String> notAgreeJoin(@NotNull(message = "社团审核表主键不能为空") @RequestParam int id){
        log.info("主键id:{}", id);
        return clubAuditingService.notAgreeJoin(id);
    }

    /**
     * 社团负责人：展示已经被审核通过的活动
     * */
    @RequiresRoles("leader")
    @GetMapping("/approvedActivity")
    @Log(title = "展示已经被审核通过的活动",dataLocationType = "link")
    R<Page> approvedActivity(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                             @RequestParam @NotNull(message = "每页最大值不能为空") int pageSize,
                             HttpServletRequest request){
        return activityService.approvedActivity(page, pageSize, request);
    }

    /**
     * 社团负责人：展示已经被审核驳回的活动
     * */
    @RequiresRoles("leader")
    @GetMapping("/notApprovedActivity")
    @Log(title = "展示已经被审核驳回的活动",dataLocationType = "link")
    R<Page> notApprovedActivity(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                                @RequestParam @NotNull(message = "每页最大值不能为空") int pageSize,
                                HttpServletRequest request){
        return activityService.notApprovedActivity(page, pageSize, request);
    }

    /**
     * 撤销举办活动的申请
     * @param activity
     * 朱彦燕
     * 测试成功
     * */
    @RequiresRoles("leader")
    @PutMapping("/cancelActivity")
    @Log(title = "社团负责人：撤销举办活动的申请", dataLocationType = "body")
    public R<String> cancelActivity(@RequestBody @Validated(Delete.class) Activity activity){
        return activityService.cancelActivity(activity);
    }

    /**
     * 社团负责人：展示已申请待审核的活动
     */
    @RequiresRoles("leader")
    @GetMapping("/reviewedActivity")
    @Log(title = "社团负责人：展示已申请待审核的活动", dataLocationType = "link")
    R<Page> reviewedActivity(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                             @RequestParam @NotNull(message = "每页最大值不能为空") int pageSize,
                             HttpServletRequest request){
        return activityService.reviewedActivity(page, pageSize, request);
    }

    /**
     * 社团负责人：展示所有待审核申请
     * */
    @GetMapping("/applyLeaderList")
    @Log(title = "社团负责人：展示所有待审核负责人申请", dataLocationType = "link")
    R<Page> applyLeaderList(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                            @RequestParam @NotNull(message = "每页最大值不能为空") int pageSize,
                            HttpServletRequest request){
        return clubLeaderService.applyList(page, pageSize, request);
    }

    /**
     * 社团负责人：同意负责人申请
     * */
    @PutMapping("/agreeLeader")
    @Log(title = "社团负责人：同意负责人申请", dataLocationType = "link")
    R<String> agreeLeader(@RequestParam @NotNull(message = "社团负责人表主键不能为空") int id){
        return clubLeaderService.agree(id);
    }

    /**
     * 社团负责人：不同意负责人申请
     * */
    @DeleteMapping("/declineLeader")
    @Log(title = "社团负责人：不同意负责人申请", dataLocationType = "link")
    R<String> declineLeader(@RequestParam @NotNull(message = "社团负责人表主键不能为空") int id){
        return clubLeaderService.decline(id);
    }


}
